package com.xbts.system.domain.filter;


import com.xbts.common.core.domain.model.LoginUser;
import com.xbts.common.exception.ServiceException;
import com.xbts.common.utils.SecurityUtils;
import com.xbts.common.utils.StringUtils;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.io.StreamCorruptedException;

@WebFilter(urlPatterns = "/*", filterName = "authFilter")
public class AccessFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(
            ServletRequest servletRequest,
            ServletResponse servletResponse,
            FilterChain filterChain) throws IOException, ServletException {
        if (StringUtils.isEmpty(servletRequest.getParameter("ticket"))) {
            System.out.println("mock a redirection to authentication system");
            //Check if the user has been authenticated
//            LoginUser loginUser = SecurityUtils.getLoginUser();
            //If user has not logged in, redirect to identity provider authentication system
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
    }
}
